package com.example.base.net

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import java.util.concurrent.atomic.AtomicBoolean

/**
 * 数据倒灌
 */
class SingleLiveData<T>:MutableLiveData<T>() {

    private var flag = AtomicBoolean(false)

    override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        super.observe(owner){
            if(flag.compareAndSet(true,false)){
                observer.onChanged(it)
            }
        }
    }

    override fun postValue(value: T) {
        flag.set(true)
        super.postValue(value)
    }
}